package com.hake.core.support.service;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hake.core.exception.AppException;
import com.hake.core.exception.BusinessException;
import com.hake.core.exception.DataBaseException;
import com.hake.core.support.mapper.BaseMapper;
import com.hake.core.support.web.PageParamsThreadLocal;
import com.hake.core.util.DataUtil;

public abstract class BaseService<T, I> implements IService<T, I> {

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private BaseMapper<T, I> baseMapper;

	protected boolean returnBoolean(int result) {
		return (result >= 1) ? true : false;
	}

	@Override
	public boolean insert(T entity) throws DataBaseException, BusinessException, AppException {
		try {
			return returnBoolean(this.baseMapper.insert(entity));
		} catch (Exception e) {
			logger.error("插入记录异常", e);
			throw new DataBaseException(e);
		}
	}

	@Override
	public boolean insertSelective(T entity) throws DataBaseException, BusinessException, AppException {
		try {
			return returnBoolean(this.baseMapper.insertSelective(entity));
		} catch (Exception e) {
			logger.error("插入记录异常", e);
			throw new DataBaseException(e);
		}
	}

	@Override
	public boolean insertBatch(List<T> entityList) throws DataBaseException, BusinessException, AppException {
		try {
			return returnBoolean(this.baseMapper.insertBatch(entityList));
		} catch (Exception e) {
			logger.error("批量插入记录异常", e);
			throw new DataBaseException(e);
		}
	}

	@Override
	public boolean deleteById(I id) throws DataBaseException, BusinessException, AppException {
		try {
			return returnBoolean(this.baseMapper.deleteById(id));
		} catch (Exception e) {
			logger.error("根据ID删除记录异常", e);
			throw new DataBaseException(e);
		}
	}

	@Override
	public boolean deleteSelective(T entity) throws DataBaseException, BusinessException, AppException {
		try {
			return returnBoolean(this.baseMapper.deleteSelective(entity));
		} catch (Exception e) {
			logger.error("更加可选条件删除记录异常", e);
			throw new DataBaseException(e);
		}
	}

	@Override
	public boolean deleteBatchIds(List<I> idList) throws DataBaseException, BusinessException, AppException {
		try {
			return returnBoolean(this.baseMapper.deleteBatchIds(idList));
		} catch (Exception e) {
			logger.error("根据ID批量删除异常", e);
			throw new DataBaseException(e);
		}
	}

	@Override
	public boolean updateById(T entity) throws DataBaseException, BusinessException, AppException {
		try {
			return returnBoolean(this.baseMapper.updateById(entity));
		} catch (Exception e) {
			logger.error("修改记录异常", e);
			throw new DataBaseException(e);
		}
	}

	@Override
	public boolean updateSelectiveById(T entity) throws DataBaseException, BusinessException, AppException {
		try {
			return returnBoolean(this.baseMapper.updateSelectiveById(entity));
		} catch (Exception e) {
			logger.error("修改记录异常", e);
			throw new DataBaseException(e);
		}
	}

	@Override
	public boolean update(T entity, T whereEntity) throws DataBaseException, BusinessException, AppException {
		try {
			return returnBoolean(this.baseMapper.update(entity, whereEntity));
		} catch (Exception e) {
			logger.error("多条件修改记录异常", e);
			throw new DataBaseException(e);
		}
	}

	@Override
	public boolean updateSelective(T entity, T whereEntity) throws DataBaseException, BusinessException, AppException {
		try {
			return returnBoolean(this.baseMapper.updateSelective(entity, whereEntity));
		} catch (Exception e) {
			logger.error("多条件修改记录异常", e);
			throw new DataBaseException(e);
		}
	}

	@Override
	public boolean updateBatchById(List<T> entityList) throws DataBaseException, BusinessException, AppException {
		try {
			return returnBoolean(this.baseMapper.updateBatchById(entityList));
		} catch (Exception e) {
			logger.error("根据ID批量修改记录异常", e);
			throw new DataBaseException(e);
		}
	}

	@Override
	public T selectById(I id) throws DataBaseException, BusinessException, AppException {
		try {
			return this.baseMapper.selectById(id);
		} catch (Exception e) {
			logger.error("根据ID检索记录异常", e);
			throw new DataBaseException(e);
		}
	}

	@Override
	public List<T> selectBatchIds(List<I> idList) throws DataBaseException, BusinessException, AppException {
		try {
			return this.baseMapper.selectBatchIds(idList);
		} catch (Exception e) {
			logger.error("根据ID批量检索记录异常", e);
			throw new DataBaseException(e);
		}
	}

	@Override
	public T selectOne(T entity) throws DataBaseException, BusinessException, AppException {
		try {
			return this.baseMapper.selectOne(entity);
		} catch (Exception e) {
			logger.error("根据多条件检索记录异常", e);
			throw new DataBaseException(e);
		}
	}

	@Override
	public int selectCount(T entity) throws DataBaseException, BusinessException, AppException {
		try {
			return this.baseMapper.selectCount(entity);
		} catch (Exception e) {
			logger.error("统计记录异常", e);
			throw new DataBaseException(e);
		}
	}

	@Override
	public List<T> selectList(T entity, String orderByField) throws DataBaseException, BusinessException, AppException {
		try {
			return baseMapper.selectList(entity, orderByField);
		} catch (Exception e) {
			logger.error("检索记录异常", e);
			throw new DataBaseException(e);
		}
	}

	@Override
	public List<T> selectList(T entity) throws DataBaseException, BusinessException, AppException {
		try {
			return baseMapper.selectList(entity, null);
		} catch (Exception e) {
			logger.error("检索记录异常", e);
			throw new DataBaseException(e);
		}
	}

	@Override
	public PageInfo<T> selectPage(T entity, String orderByField) throws DataBaseException, BusinessException, AppException {
		try {
			Page<T> page = PageHelper.startPage(PageParamsThreadLocal.getPageNum(), PageParamsThreadLocal.getPageSize(), orderByField) ;
			page.clear();
			baseMapper.selectPage(entity);
			return new PageInfo<T>(page) ;
		} catch (Exception e) {
			logger.error("检索记录并翻页异常", e);
			throw new DataBaseException(e);
		}
	}
	
	@Override
	public PageInfo<T> selectPage(T entity) throws DataBaseException, BusinessException, AppException {
		try {
			return selectPage(entity, null);
		} catch (Exception e) {
			logger.error("检索记录并翻页异常", e);
			throw new DataBaseException(e);
		}
	}
	
	@Override
	public PageInfo<T> selectPage(Map<String, Object> params) throws DataBaseException, BusinessException, AppException {
		try {
			if (DataUtil.isEmpty(params.get("pageNum"))) {
				params.put("pageNum", 1);
			}
			if (DataUtil.isEmpty(params.get("pageSize"))) {
				params.put("pageSize", 10);
			}
			if (DataUtil.isEmpty(params.get("orderBy"))) {
				params.put("orderBy", "id desc");
			}
			
			Page<T> page = PageHelper.startPage(params) ;
			baseMapper.selectPage(params) ;
			return new PageInfo<T>(page) ;
		} catch (Exception e) {
			logger.error("检索记录并翻页异常", e);
			throw new DataBaseException(e);
		}
	}

}
